<?php
/**
 * Reload Patient Monitoring Grid
 * 
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy\MedicalCenter\Patient
 * @license
 * 
 */
use Nawa\MedicalStudy\MedicalCenter\Patient;
$xview = Xview::instance();
$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$patientid = $xview->val('patientid');
$itemid = $xview->val('itemid',null);
$summary = $xview->val('summary',false);
$data = array();
$monitoringValidated = false;
try {
	$patient = Fox\Dao::load(Patient::className(),$msid.':'.$mcid.':'.$patientid);
	if(empty($itemid)) {
		// We should only display a summary
		foreach($patient->monitorings() as $monitoring) {
			$monitoringData = $monitoring->getData();
			$visit = $patient->findVisits($msid.':'.$mcid.':'.$patientid.':'.$monitoring->itemid);
			if($visit->exists()) {
				$monitoringData['visit_date'] = $visit->visit_date;
			}
			$data[$monitoring->itemid] = $monitoringData;
		}
	} else {
		foreach($patient->monitorings() as $monitoring) {
			if(empty($itemid) || (!empty($itemid) && $itemid == $monitoring->itemid)) {
				$monitoringData = $monitoring->getData();
				if($monitoringData['validated']) {
					$monitoringValidated = true;
				}
				$data[] = $monitoringData;
			} 
		}
	}
} catch(Fox\Object\Exception\NotExist $e) {
	//@TODO do something
}

// Monitoring List
$grid = $xview->component('medicalstudies','monitorings',array(
	'msid'		=> $msid,
	'mcid'		=> $mcid,
	'patientid' => $patientid,
	'itemid' => $itemid,
	'noadd'		=> $monitoringValidated
));

$grid->initialize($data);
echo $grid->display();